#include "menu.h"   
#include "math.h"
#include "delay.h"
#include "stdlib.h"
#include "stdio.h"
#include "draw_api.h"
#include <HMI.H>
void ShowStars(void);
extern uchar func_index;
key_table table[30]=
{
	//第0层
	{0,0,0,1,(*fun_0)},
	
    //第1层
	{1,4,2, 5,(*fun_a1)},
	{2,1,3, 9,(*fun_b1)},
	{3,2,4,13,(*fun_c1)},		
	{4,3,1, 0,(*fun_d1)},
	
    //第2层
	{5,8,6,17,(*fun_a21)},					
	{6,5,7,18,(*fun_a22)},
	{7,6,8,19,(*fun_a23)},						                	
	{8,7,5, 1,(*fun_a24)},
	
	{ 9,12,10,20,(*fun_b21)},					
	{10, 9,11,21,(*fun_b22)},
	{11,10,12,22,(*fun_b23)},						                	
	{12,11, 9, 2,(*fun_b24)},
	
	{13,16,14,23,(*fun_c21)},					
	{14,13,15,24,(*fun_c22)},				                	
	{15,14,16,25,(*fun_c23)},				                	
	{16,15,13, 3,(*fun_c24)},
	
    //第3层
	{17,17,17,5,(*fun_a31)},			                	
	{18,18,18,6,(*fun_a32)},		                	
	{19,19,19,7,(*fun_a33)},
	
	{20,20,20, 9,(*fun_b31)},				                	
	{21,21,21,10,(*fun_b32)},			                	
	{22,22,22,11,(*fun_b33)},
	
	{23,23,23,13,(*fun_c31)},			                	
	{24,24,24,14,(*fun_c32)},			                	
	{25,25,25,15,(*fun_c33)},								
};

/*********第1层***********/
void fun_a1()   
{	
	SetFontSize(0);
	DrawString(0,0,">");
	DrawString(16,0,"Animation");
	DrawString(16,2,"Control");
	DrawString(16,4,"Device Info");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();
}

void fun_b1()   
{	
	SetFontSize(0);
	DrawString(0,2,">");
	DrawString(16,0,"Animation");
	DrawString(16,2,"Control");
	DrawString(16,4,"Device Info");
	DrawString(16,6,"return");	
	UpdateScreen();
	ClearScreen();
}

void fun_c1()     
{	
	SetFontSize(0);
	DrawString(0,4,">");
	DrawString(16,0,"Animation");
	DrawString(16,2,"Control");
	DrawString(16,4,"Device Info");
	DrawString(16,6,"return");	
	UpdateScreen();
	ClearScreen();	
}

void fun_d1()     
{	
	SetFontSize(0);
	DrawString(0,6,">");
	DrawString(16,0,"Animation");
	DrawString(16,2,"Control");
	DrawString(16,4,"Device Info");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();
}

/*********第2层***********/
void fun_a21()     
{	
	SetFontSize(0);
	DrawString(0,0,">");
	DrawString(16,0,"Starry sky");
	DrawString(16,2,"Time");
	DrawString(16,4,"Planet");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();	
}

void fun_a22()      
{	
	SetFontSize(0);
	DrawString(0,2,">");
	DrawString(16,0,"Starry sky");
	DrawString(16,2,"Time");
	DrawString(16,4,"Planet");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();		
}

void fun_a23()    
{	
	SetFontSize(0);
	DrawString(0,4,">");
	DrawString(16,0,"Starry sky");
	DrawString(16,2,"Time");
	DrawString(16,4,"Planet");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();		
}

void fun_a24()    
{	
	SetFontSize(0);
	DrawString(0,6,">");
	DrawString(16,0,"Starry sky");
	DrawString(16,2,"Time");
	DrawString(16,4,"Planet");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();	
}

void fun_b21()    
{	
	ClearScreen();
	SetFontSize(0);
	DrawString(0,0,">");
	DrawString(16,0,"MPU6050");
	DrawString(16,2,"EC11");
	DrawString(16,4,"Rocker");
	DrawString(16,6,"return");	
	UpdateScreen();
	ClearScreen();		
}

void fun_b22()    
{	
	SetFontSize(0);
	DrawString(0,2,">");
	DrawString(16,0,"MPU6050");
	DrawString(16,2,"EC11");
	DrawString(16,4,"Rocker");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();		
}

void fun_b23()    
{	
	SetFontSize(0);
	DrawString(0,4,">");
	DrawString(16,0,"MPU6050");
	DrawString(16,2,"EC11");
	DrawString(16,4,"Rocker");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();		
}

void fun_b24()    
{	
	SetFontSize(0);
	DrawString(0,6,">");
	DrawString(16,0,"MPU6050");
	DrawString(16,2,"EC11");
	DrawString(16,4,"Rocker");
	DrawString(16,6,"return");	
	UpdateScreen();
	ClearScreen();		
}

void fun_c21()    
{	
	SetFontSize(0);
	DrawString(0,0,">");
	DrawString(16,0,"PWM text");
	DrawString(16,2,"Volume");
	DrawString(16,4,"Version");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();	
}

void fun_c22()    
{	
	SetFontSize(0);
	DrawString(0,2,">");
	DrawString(16,0,"PWM text");
	DrawString(16,2,"Volume");
	DrawString(16,4,"Version");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();		
}

void fun_c23()    
{	
	SetFontSize(0);
	DrawString(0,4,">");
	DrawString(16,0,"PWM text");
	DrawString(16,2,"Volume");
	DrawString(16,4,"Version");
	DrawString(16,6,"return");	
	UpdateScreen();
	ClearScreen();		
}

void fun_c24()    
{	
	SetFontSize(0);
	DrawString(0,6,">");
	DrawString(16,0,"PWM text");
	DrawString(16,2,"Volume");
	DrawString(16,4,"Version");
	DrawString(16,6,"return");
	UpdateScreen();
	ClearScreen();		
}

/*********第3层***********/
void fun_a31()    
{	
	ShowStars();	
//	func_index = 5;	
}

void fun_a32()    
{	
	ShowWatch();
//	func_index = 6;	
	ClearScreen();	
//	SetFontSize(0);
//	DrawString(0,0,"ChongZuo Weather");
//	DrawString(0,2,"Sunny");
//	DrawString(0,4,"");
//	DrawString(0,6,"Enter to Return");			
//	UpdateScreen();
//	ClearScreen();		
}

void fun_a33()    
{	
	int i;
	for(i=0;i<90;i=i+1)
	{
		TypeXY temp1;					//
		SetAngle(i);			//画图的角度
		SetRotateCenter(63,32);//旋转中心
		
		temp1=GetRotateXY(63,32+30);
		
		DrawFillCircle(temp1.x,temp1.y,5);//整圆 小
		
		DrawCircle(63,32,30);
		DrawFillCircle(63,32,10);
		UpdateScreen();
		ClearScreen();
	}	
		
			//绕点
	for(i=0;i<720;i=i+10)//转720度2圈
	{
		TypeXY temp1;
		SetAngle(i+63);					//设置角度
		SetRotateCenter(63,32);			//设置圆心
		
		temp1=GetRotateXY(63,32+30);		//讲已知坐标旋转角度
		DrawFillCircle(temp1.x,temp1.y,5);//画出算出的位置
		DrawCircle(63,32,30);			//画出轨迹  外圆
		DrawFillCircle(63,32,10);		//填充中心
		UpdateScreen();					//更新屏幕
		ClearScreen();
	}
	
	//向右边平移
	for(i=0;i<96;i++)
	{
		TypeXY temp1;					//其实就是上面继续旋转
		SetAngle(i+63);				//画图的时候x+偏移量
		SetRotateCenter(63+i,31);
		temp1=GetRotateXY(63+i,31+30);
		DrawFillCircle(temp1.x,temp1.y,5);
		DrawCircle(63+i,31,30);
		DrawFillCircle(63+i,31,10);
		UpdateScreen();
		ClearScreen();
	}
	func_index = 7;	
//	SetFontSize(0);
//	DrawString(0,0,"NanNing Weather");
//	DrawString(0,2,"Sunny");
//	DrawString(0,4,"");
//	DrawString(0,6,"Enter to Return");	
//	UpdateScreen();
//	ClearScreen();		
}

void fun_b31()    
{	
//	SetFontSize(0);
//	DrawString(0,0,"*** Music ***");
//	DrawString(0,2,"WindyHill");
//	DrawString(0,4,"");
//	DrawString(0,6,"Enter to Return");
//	UpdateScreen();
//	ClearScreen();		
}

void fun_b32()    
{	
//	SetFontSize(0);
//	DrawString(0,0,"*** Music ***");
//	DrawString(0,2,"New Boy");
//	DrawString(0,4,"");
//	DrawString(0,6,"Enter to Return");																				
}

void fun_b33()    
{	
//	SetFontSize(0);
//	DrawString(0,0,"*** Music ***");
//	DrawString(0,2,"Kill The Rain");
//	DrawString(0,4,"");
//	DrawString(0,6,"Enter to Return");
//	UpdateScreen();
//	ClearScreen();	
}

void fun_c31()    
{	
//	SetFontSize(0);
//	DrawString(0,0,"WiFI info");
//	DrawString(0,2,"ssid:xxx");
//	DrawString(0,4,"passwd:xxx");
//	DrawString(0,6,"Enter to Return");	
//	UpdateScreen();
//	ClearScreen();	
}

void fun_c32()    
{	
	SetFontSize(0);
	DrawString(0,0,"Volume info");
	DrawString(0,2,"volume:xx");
	DrawString(0,4,"");
	DrawString(0,6,"Enter to Return");
	UpdateScreen();
	ClearScreen();	
}

void fun_c33()    
{	
	SetFontSize(0);
	DrawString(0,0,"Version info");
	DrawString(0,2,"version:V1.0.0");
	DrawString(0,4,"");
	DrawString(0,6,"Enter to Return");
	UpdateScreen();
	ClearScreen();	
}



/*********第0层***********/
void fun_0()
{
	SetFontSize(0);
	DrawString(3,0,"XWLTE Menu Test");
	DrawString((127-7*8)/2,2,"SSD1036");
	DrawString(16,4,"GUET_BADBOY");
	DrawString(10,6,"Enter to Test");		
	DrawRoundRect(8,50,108,64-50,5);//圆角矩形
	DrawRoundRect(0,0,128,64,5);//圆角矩形
	DrawRoundRect(5,50,108,64-50,5);//圆角矩形
	UpdateScreen();
	ClearScreen();	
}


/////////////////////////////////////////////////////////////////////////////////////////////
//星空动画
void ShowStars(void)
{
	int i;
	int count=0;
	int fps = 80;//频率
	typedef struct START
	{
		short x;
		short y;
		short speed;
		unsigned char speedcount;
		unsigned char isexist;
	}Star;
	
	Star star[128]={0};
	srand(2);
	for(i=0;i<128;i++)
	{
		if(star[i].isexist==0)
		{
			//设置128个()星星的初始信息
			star[i].x=rand()%127;		//随机生成初始x坐标
			star[i].y=rand()%64;		//随机生成y的坐标
			star[i].speedcount=0;
			star[i].speed=rand()%6+1;	//1-8的数 速度
			star[i].isexist=1;
		}
	}
	while(1)
	{
		if(FrameRateUpdateScreen(fps)==1)	//在此函数中定时刷新屏
		{
			count++;
			if(count>=fps*5)   			//10秒钟时间到达之后跳出循环结束动画
			{
				func_index = 5;
				return;
			}
		}
		//此段函数一直在运行
		//依次画出128个星星
		for(i=0;i<128;i++)
		{
			//如果这一个星星已经移动到退出屏幕界面
			//则在最左侧重新生成一颗新星星
			if(star[i].isexist==0) 
			{
				star[i].x=0;
				star[i].y=rand()%64;
				star[i].speed=rand()%6+1;
				star[i].speedcount=0;		
				star[i].isexist=1;
			}
			else
			{
				star[i].speedcount++;
				if(star[i].x>=124)			//标记已经退出屏幕 
					star[i].isexist=0;
				
				//清除上一个时刻画的星星(的尾巴) 不管有没有操作 都进行清除操作
				SetDrawColor(pix_black);//设为黑色
				DrawPixel(star[i].x,star[i].y);//清除上一时刻的星星尾巴
			
				SetDrawColor(pix_white);//设为白色
				if(star[i].speedcount==star[i].speed)	//运行时间到了一定的长度
				{
					star[i].speedcount=0;				//复位运行时间并向右移一格
					star[i].x+=1;						//总之星星的结束需要在这经历124次
				}										//只不过有的更快 就能移动更快
				
				//从头到尾画出整条星星 不管星星是否已经变化 速度越快 长度最大  speed=1速度最大 长度最长
				DrawLine(star[i].x, star[i].y, star[i].x+(6/star[i].speed)-1, star[i].y);
			}	
		}
	}
	
}

////////////////////////////////
//手表动画
void ShowWatch(void)
{
	int i,j,z;
	int count=0;
	
	for(i=0;i<12;i++)
		for(j=0;j<60;j++)
			for(z=0;z<60;z++)
			{	
				ClearScreen();	
				RoundClock(i,j,z);
				count++;
				UpdateScreen();
				if(count>=800)
				{
					func_index = 6;
					return;
					
				}
			}
}
